<template>
  <van-popup v-model="show" position="bottom" :style="{ height: '100%' }" :get-container="getContainer" @open="handleSearch">
    <van-nav-bar
      title="选择订单"
      left-text="返回"
      :right-text="multiple?selectAllFlag?'取消全选':'全选':''"
      left-arrow
      @click-left="doClose"
      @click-right="handleSelectAll"
    />
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-search v-model="page.query.keyword" show-action placeholder="请输入关键字查询" @search="handleSearch(null)">
        <template #action>
          <div @click="handleSearch(null)">搜索</div>
        </template>
      </van-search>
      <van-list
        v-model="tableLoading"
        :finished="finished"
        finished-text="没有更多了"
        @load="doPage"
      >
        <van-checkbox-group v-if="multiple" v-model="orderIds">
          <van-cell-group>
            <van-cell v-for="(item,index) in tableData" :key="index" clickable @click="toggleCheckbox(item,index)">
              <template #right-icon>
                <van-checkbox ref="checkboxes" :name="item.id" :disabled="getDisabled(item)" />
              </template>
              <template #title>
                <div class="title">
                  <div v-html="highlightText(item.title, page.query.keyword)" />
                </div>
                <div class="title right primary">
                  <div v-html="highlightText(item.quantityNum, page.query.keyword)" />
                </div>
                <div class="right_tag">
                  <order-state-tag v-model="item.orderState" />
                  <priority-tag v-model="item.priority" />
                </div>
              </template>
              <template #label>
                <div class="desc">
                  <div v-html="highlightText(item.productAttribute, page.query.keyword)" />
                  <div v-html="highlightText(item.orderDate, page.query.keyword)" />
                </div>
              </template>
            </van-cell>
          </van-cell-group>
        </van-checkbox-group>
        <van-radio-group v-else v-model="orderId">
          <van-cell v-for="(item,index) in tableData" :key="index" @click="handleChangeOrder(item)">
            <template #title>
              <div v-if="item.disabled" class="title_disabled">
                <div v-html="highlightText(item.title, page.query.keyword)" />
              </div>
              <div v-else class="title">
                <div v-html="highlightText(item.title, page.query.keyword)" />
              </div>
              <div class="title right primary">
                <div v-html="highlightText(item.quantityNum, page.query.keyword)" />
              </div>
              <div class="right_tag">
                <order-state-tag v-model="item.orderState" />
                <priority-tag v-model="item.priority" />
              </div>
            </template>
            <template #label>
              <div class="desc">
                <div v-html="highlightText(item.productAttribute, page.query.keyword)" />
                <div v-html="highlightText(item.orderDate, page.query.keyword)" />
              </div>
            </template>
            <template #right-icon>
              <van-radio :name="item.id" :disabled="getDisabled(item)" />
            </template>
          </van-cell>
        </van-radio-group>
      </van-list>
    </van-pull-refresh>
    <div class="bottom_button">
      <van-button round block type="danger" @click="confirmSelectOrder">确定</van-button>
    </div>
  </van-popup>
</template>

<script>
import { selectSaleOrderPage } from '@/api/saleOrder'
import OrderStateTag from '@/components/tag/OrderStateTag.vue'
import PriorityTag from '@/components/tag/PriorityTag.vue'
import { highlightText } from '@/utils/util'

export default {
  name: 'SelectOrder',
  components: { PriorityTag, OrderStateTag },
  props: {
    multiple: {
      type: Boolean,
      default: false
    },
    visible: {
      type: Boolean,
      default: false
    },
    disableIds: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      show: this.visible,
      orderId: '',
      orderIds: [],
      showSubmit: false,
      activeTab: 0,
      tableLoading: false,
      finished: true,
      tableData: [],
      search: false,
      refreshing: false,
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          keyword: '',
          customerId: '',
          shipmentStateList: [],
          paymentStateList: [],
          orderStateList: []
        }
      },
      selectAllFlag: false
    }
  },
  methods: {
    highlightText,
    doPage() {
      this.tableLoading = true
      selectSaleOrderPage(this.page).then(resp => {
        if (resp.success) {
          if (this.refreshing) {
            this.tableData = []
            this.refreshing = false
          }
          this.tableData = [...this.tableData, ...resp.data.records]
          this.page.total = resp.data.total
          this.finished = this.tableData.length >= resp.data.total
          for (let i = 0; i < resp.data.records.length; i++) {
            const tableDatum = resp.data.records[i]
            tableDatum.title = tableDatum.customerName + ' ' + tableDatum.productInfo.moldName
            tableDatum.quantityNum = tableDatum.quantity + ' ' + tableDatum.productInfo.unit
            tableDatum.productAttribute =
                '颜色:' + tableDatum.productInfo.color +
                ',材质:' + tableDatum.productInfo.material +
                ',尺码:' + tableDatum.productInfo.size +
                ',长度:' + tableDatum.productInfo.length
          }
        }
      }).catch(() => {
        this.finished = true
      }).finally(() => {
        this.tableLoading = false
        this.page.page++
      })
    },
    handleSearch(query) {
      this.tableData = []
      this.page.page = 1
      this.finished = false
      if (query) {
        this.page.query.customerId = query.customerId
        this.page.query.shipmentStateList = query.shipmentStateList
        this.page.query.orderStateList = query.orderStateList
        this.page.query.paymentStateList = query.paymentStateList
      }
      this.doPage()
    },
    onRefresh() {
      this.page.page = 1
      this.doPage()
    },
    toggleCheckbox(item, index) {
      if (!item.disabled) {
        this.$refs.checkboxes[index].toggle()
      }
    },
    handleChangeOrder(row) {
      this.orderId = row.id
    },
    confirmSelectOrder() {
      if (this.multiple) {
        const orderList = this.tableData.filter(e => this.orderIds.indexOf(e.id) !== -1)
        this.tableData = []
        this.orderIds = []
        this.$emit('close', orderList)
      } else {
        const orderInfo = this.tableData.filter(e => this.orderId.indexOf(e.id) !== -1)[0]
        this.orderIds = ''
        this.$emit('close', orderInfo)
      }
    },
    doClose() {
      this.orderId = ''
      this.orderIds = []
      this.tableData = []
      this.$emit('close')
    },
    getContainer() {
      return document.body
    },
    getDisabled(row) {
      const disabled = this.disableIds.indexOf(row.id) !== -1
      row.disabled = disabled
      return disabled
    },
    handleSelectAll() {
      if (!this.multiple) {
        return
      }
      this.orderIds = []
      for (let i = 0; i < this.tableData.length; i++) {
        if (this.selectAllFlag) {
          this.$refs.checkboxes[i].toggle(false)
        } else {
          const item = this.tableData[i]
          if (!item.disabled) {
            this.orderIds.push(item.id)
            this.$refs.checkboxes[i].toggle(true)
          }
        }
      }
      this.selectAllFlag = !this.selectAllFlag
    }
  }
}
</script>

<style scoped>

</style>
